//
//  UIViewController+Category.m
//  GK
//
//  Created by Seven on 2019/2/18.
//  Copyright © 2019 Seven. All rights reserved.
//

#import "UIViewController+Category.h"

@implementation UIViewController(add)

+(void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        @autoreleasepool {
            [self swizzleInstanceMethod:@selector(didMoveToParentViewController:) with:@selector(gk_didMoveToParentViewController:)];
        }
    });
}

const NSString *willDeallocBlockKey = @"willDeallocBlockKey";
- (void)setWillDeallocBlock:(void (^)(void))willDeallocBlock{
    objc_setAssociatedObject(self, (__bridge const void *)(willDeallocBlockKey), willDeallocBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)(void))willDeallocBlock{
    return objc_getAssociatedObject(self, (__bridge const void *)(willDeallocBlockKey));;
}

- (void)gk_didMoveToParentViewController:(UIViewController *)parent{
    [self gk_didMoveToParentViewController:parent];
    if (!parent) {
        [self willDealloc];
    }
}

- (void)willDealloc{
    if (self.willDeallocBlock){
        self.willDeallocBlock();
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



#pragma mark - trackNameKey
const NSString *trackNameKey = @"trackNameKey";
- (void)setTrackName:(NSString *)trackName{
    objc_setAssociatedObject(self, (__bridge const void *)(trackNameKey), trackName, OBJC_ASSOCIATION_COPY);
}
- (NSString *)trackName{
    return objc_getAssociatedObject(self, (__bridge const void *)(trackNameKey));
}

- (void)tt_viewDidAppear:(BOOL)animated{
    [self tt_viewDidAppear:animated];
    if (self.trackName.length>0) {
        [ITTrackManager setPageName:self.trackName];
    }
}


- (void)onDoubleClick{
    
}

@end
